﻿using kad2SimSoft.Classes;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace kad2SimSoft
{
    /// <summary>
    /// Interaktionslogik für ProductionPlanningControl.xaml
    /// </summary>
    public partial class ProductionPlanningControl : UserControl
    {
        private List<Endproduct> endproducts;
        private List<PurchasedPart> purchasedParts;
        private List<ManufacturedPart> manufacturedParts;
        private List<Forecast> forecasts;
        List<OrdersInWork> ordersInWork;
        List<WaitinglistWorkplace> waitingListWorkplace;
        List<Workplace> workplaces;
        public int fertig = 0;

        public ProductionPlanningControl(List<PurchasedPart> purchasedParts, List<ManufacturedPart> manufacturedParts, 
            List<Endproduct> endproducts, List<Forecast> forecasts, List<WaitinglistWorkplace> waitingListWorkplace, List<Workplace> workplaces, List<OrdersInWork> ordersInWork)
        {
            InitializeComponent();
            this.endproducts = endproducts;
            this.manufacturedParts = manufacturedParts;
            this.ordersInWork = ordersInWork;
            this.workplaces = workplaces;
            DataContext = this;
            Init(purchasedParts, manufacturedParts, endproducts, forecasts, waitingListWorkplace, workplaces);
        }

        public void Init(List<PurchasedPart> purchasedParts, List<ManufacturedPart> manufacturedParts, List<Endproduct> endproducts, 
            List<Forecast> forecasts, List<WaitinglistWorkplace> waitingListWorkplace, List<Workplace> workplaces)
        {
            //var item = manufacturedParts.FirstOrDefault(o => o.Id == 49);
            //if (item != null)
            //    this.txtE51LagerbestEndeVorperiode.Text = item.Amount.ToString();        


            lblWarningTab1.Visibility = Visibility.Hidden;
            lblWarningTab1_Copy.Visibility = Visibility.Hidden;
            lblWarningTab1_Copy1.Visibility = Visibility.Hidden;

 

            //Vertriebswunsch
            Forecast forecast = forecasts.FirstOrDefault(o => o.Product.Id == 1);
            this.txtP1Vertriebswunsch.Text = forecast.Count.ToString();

            Forecast forecast2 = forecasts.FirstOrDefault(o => o.Product.Id == 2);
            this.txtP2Vertriebswunsch.Text = forecast2.Count.ToString();

            Forecast forecast3 = forecasts.FirstOrDefault(o => o.Product.Id == 3);
            this.txtP3Vertriebswunsch.Text = forecast3.Count.ToString();

            //Write Amount stock
            this.txtP1LagerbestEndeVorperiode.Text = AmountEndProduct(1, endproducts);
            this.txtE26LagerbestEndeVorperiode.Text = AmountManufacturedProduct(26, manufacturedParts);
            this.txtE51LagerbestEndeVorperiode.Text = AmountManufacturedProduct(51, manufacturedParts);
            this.txtE16LagerbestEndeVorperiode.Text = AmountManufacturedProduct(16, manufacturedParts);
            this.txtE17LagerbestEndeVorperiode.Text = AmountManufacturedProduct(17, manufacturedParts);
            this.txtE50LagerbestEndeVorperiode.Text = AmountManufacturedProduct(50, manufacturedParts);
            this.txtE04LagerbestEndeVorperiode.Text = AmountManufacturedProduct(4, manufacturedParts);
            this.txtE10LagerbestEndeVorperiode.Text = AmountManufacturedProduct(10, manufacturedParts);
            this.txtE49LagerbestEndeVorperiode.Text = AmountManufacturedProduct(49, manufacturedParts);
            this.txtE07LagerbestEndeVorperiode.Text = AmountManufacturedProduct(7, manufacturedParts);
            this.txtE13LagerbestEndeVorperiode.Text = AmountManufacturedProduct(13, manufacturedParts);
            this.txtE18LagerbestEndeVorperiode.Text = AmountManufacturedProduct(18, manufacturedParts);

            this.txtP2LagerbestEndeVorperiode.Text = AmountEndProduct(2, endproducts);
            //this.txtE26_P2LagerbestEndeVorperiode.Text = AmountManufacturedProduct(26, manufacturedParts);
            this.txtE56LagerbestEndeVorperiode.Text = AmountManufacturedProduct(56, manufacturedParts);
            //this.txtE16_P2LagerbestEndeVorperiode.Text = AmountManufacturedProduct(16, manufacturedParts);
            //this.txtE17_P2LagerbestEndeVorperiode.Text = AmountManufacturedProduct(17, manufacturedParts);
            this.txtE55LagerbestEndeVorperiode.Text = AmountManufacturedProduct(55, manufacturedParts);
            this.txtE05LagerbestEndeVorperiode.Text = AmountManufacturedProduct(5, manufacturedParts);
            this.txtE11LagerbestEndeVorperiode.Text = AmountManufacturedProduct(11, manufacturedParts);
            this.txtE54LagerbestEndeVorperiode.Text = AmountManufacturedProduct(54, manufacturedParts);
            this.txtE08LagerbestEndeVorperiode.Text = AmountManufacturedProduct(8, manufacturedParts);
            this.txtE14LagerbestEndeVorperiode.Text = AmountManufacturedProduct(14, manufacturedParts);
            this.txtE19LagerbestEndeVorperiode.Text = AmountManufacturedProduct(19, manufacturedParts);

            this.txtP3LagerbestEndeVorperiode.Text = AmountEndProduct(3, endproducts);
            //this.txtE26_P3LagerbestEndeVorperiode.Text = AmountManufacturedProduct(26, manufacturedParts);
            this.txtE31LagerbestEndeVorperiode.Text = AmountManufacturedProduct(31, manufacturedParts);
            //this.txtE16_P3LagerbestEndeVorperiode.Text = AmountManufacturedProduct(16, manufacturedParts);
            //this.txtE17_P3LagerbestEndeVorperiode.Text = AmountManufacturedProduct(17, manufacturedParts);
            this.txtE30LagerbestEndeVorperiode.Text = AmountManufacturedProduct(30, manufacturedParts);
            this.txtE06LagerbestEndeVorperiode.Text = AmountManufacturedProduct(6, manufacturedParts);
            this.txtE12LagerbestEndeVorperiode.Text = AmountManufacturedProduct(12, manufacturedParts);
            this.txtE29LagerbestEndeVorperiode.Text = AmountManufacturedProduct(29, manufacturedParts);
            this.txtE09LagerbestEndeVorperiode.Text = AmountManufacturedProduct(9, manufacturedParts);
            this.txtE15LagerbestEndeVorperiode.Text = AmountManufacturedProduct(15, manufacturedParts);
            this.txtE20LagerbestEndeVorperiode.Text = AmountManufacturedProduct(20, manufacturedParts);


            //Write Amount OrdersInWork
            this.txtP1AuftrInBearbeitung.Text = OrdersInWorkAmount(1, ordersInWork);
            this.txtE26AuftrInBearbeitung.Text = OrdersInWorkAmount(26, ordersInWork);
            this.txtE51AuftrInBearbeitung.Text = OrdersInWorkAmount(51, ordersInWork);
            this.txtE16AuftrInBearbeitung.Text = OrdersInWorkAmount(16, ordersInWork);
            this.txtE17AuftrInBearbeitung.Text = OrdersInWorkAmount(17, ordersInWork);
            this.txtE50AuftrInBearbeitung.Text = OrdersInWorkAmount(50, ordersInWork);
            this.txtE04AuftrInBearbeitung.Text = OrdersInWorkAmount(4, ordersInWork);
            this.txtE10AuftrInBearbeitung.Text = OrdersInWorkAmount(10, ordersInWork);
            this.txtE49AuftrInBearbeitung.Text = OrdersInWorkAmount(49, ordersInWork);
            this.txtE07AuftrInBearbeitung.Text = OrdersInWorkAmount(7, ordersInWork);
            this.txtE13AuftrInBearbeitung.Text = OrdersInWorkAmount(13, ordersInWork);
            this.txtE18AuftrInBearbeitung.Text = OrdersInWorkAmount(18, ordersInWork);

            this.txtP2AuftrInBearbeitung.Text = OrdersInWorkAmount(2, ordersInWork);
            //this.txtE26_P2AuftrInBearbeitung.Text = OrdersInWorkAmount(26, ordersInWork);
            this.txtE56AuftrInBearbeitung.Text = OrdersInWorkAmount(56, ordersInWork);
            //this.txtE16_P2AuftrInBearbeitung.Text = OrdersInWorkAmount(16, ordersInWork);
            //this.txtE17_P2AuftrInBearbeitung.Text = OrdersInWorkAmount(17, ordersInWork);
            this.txtE55AuftrInBearbeitung.Text = OrdersInWorkAmount(55, ordersInWork);
            this.txtE05AuftrInBearbeitung.Text = OrdersInWorkAmount(5, ordersInWork);
            this.txtE11AuftrInBearbeitung.Text = OrdersInWorkAmount(11, ordersInWork);
            this.txtE54AuftrInBearbeitung.Text = OrdersInWorkAmount(54, ordersInWork);
            this.txtE08AuftrInBearbeitung.Text = OrdersInWorkAmount(8, ordersInWork);
            this.txtE14AuftrInBearbeitung.Text = OrdersInWorkAmount(14, ordersInWork);
            this.txtE19AuftrInBearbeitung.Text = OrdersInWorkAmount(19, ordersInWork);

            this.txtP3AuftrInBearbeitung.Text = OrdersInWorkAmount(3, ordersInWork);
            //this.txtE26_P3AuftrInBearbeitung.Text = OrdersInWorkAmount(26, ordersInWork);
            this.txtE31AuftrInBearbeitung.Text = OrdersInWorkAmount(31, ordersInWork);
            //this.txtE16_P3AuftrInBearbeitung.Text = OrdersInWorkAmount(16, ordersInWork);
            //this.txtE17_P3AuftrInBearbeitung.Text = OrdersInWorkAmount(17, ordersInWork);
            this.txtE30AuftrInBearbeitung.Text = OrdersInWorkAmount(30, ordersInWork);
            this.txtE06AuftrInBearbeitung.Text = OrdersInWorkAmount(6, ordersInWork);
            this.txtE12AuftrInBearbeitung.Text = OrdersInWorkAmount(12, ordersInWork);
            this.txtE29AuftrInBearbeitung.Text = OrdersInWorkAmount(29, ordersInWork);
            this.txtE09AuftrInBearbeitung.Text = OrdersInWorkAmount(9, ordersInWork);
            this.txtE15AuftrInBearbeitung.Text = OrdersInWorkAmount(15, ordersInWork);
            this.txtE20AuftrInBearbeitung.Text = OrdersInWorkAmount(20, ordersInWork);


            //Write Amount WatingListWorkplace
            this.txtP1AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(1, workplaces);
            this.txtE26AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(26, workplaces);
            this.txtE51AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(51, workplaces);
            this.txtE16AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(16, workplaces);
            this.txtE17AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(17, workplaces);
            this.txtE50AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(50, workplaces);
            this.txtE04AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(4, workplaces);
            this.txtE10AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(10, workplaces);
            this.txtE49AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(49, workplaces);
            this.txtE07AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(7, workplaces);
            this.txtE13AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(13, workplaces);
            this.txtE18AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(18, workplaces);

            this.txtP2AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(2, workplaces);
            this.txtE56AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(56, workplaces);
            this.txtE55AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(55, workplaces);
            this.txtE05AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(5, workplaces);
            this.txtE11AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(11, workplaces);
            this.txtE54AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(54, workplaces);
            this.txtE08AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(8, workplaces);
            this.txtE14AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(14, workplaces);
            this.txtE19AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(19, workplaces);

            this.txtP3AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(3, workplaces);
            this.txtE31AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(31, workplaces);
            this.txtE30AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(30, workplaces);
            this.txtE06AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(6, workplaces);
            this.txtE12AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(12, workplaces);
            this.txtE29AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(29, workplaces);
            this.txtE09AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(9, workplaces);
            this.txtE15AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(15, workplaces);
            this.txtE20AuftrInWarteschlange.Text = WaitingListWorplaceAmountProducts(20, workplaces);

            //Write Amount Planned Stock Amount. Quals Planned amount of Bicycles.
            this.txtP1GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE26GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE51GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE16GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE17GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE50GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE04GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE10GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE49GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE07GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE13GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;
            this.txtE18GeplLagerbestand.Text = this.txtP1Vertriebswunsch.Text;

            this.txtP2GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE26_P2GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE56GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE16_P2GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE17_P2GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE55GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE05GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE11GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE54GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE08GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE14GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;
            this.txtE19GeplLagerbestand.Text = this.txtP2Vertriebswunsch.Text;

            this.txtP3GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE26_P3GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE31GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE16_P3GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE17_P3GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE30GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE06GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE12GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE29GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE09GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE15GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
            this.txtE20GeplLagerbestand.Text = this.txtP3Vertriebswunsch.Text;
                        
        }

        private string AmountManufacturedProduct(int id, List<ManufacturedPart> manufacturedParts)
        {

            ManufacturedPart manufacturedPart = manufacturedParts.FirstOrDefault(o => o.Id == id);
            if (manufacturedPart != null)
            {
                return manufacturedPart.Amount.ToString();
            }
            else
            {
                return null;
            }
             
        }

        private string AmountEndProduct(int id, List<Endproduct> endproducts)
        {

            Endproduct endproduct = endproducts.FirstOrDefault(o => o.Id == id);
            if (endproduct != null)
            {
                return endproduct.Amount.ToString();
            }
            else
            {
                return null;
            }

        }

        private string WaitingListWorplaceAmountProducts(int itemId, List<Workplace> workplaces)
        {
            int amountItem = 0;

            foreach (Workplace workplace in workplaces)
            {
                foreach (WaitinglistWorkplace waitinglist in workplace.WaitingList)
                {
                    if (waitinglist.Item.Equals(itemId))
                    {
                        amountItem += waitinglist.Amount;
                    }
                }               

            }

            return amountItem.ToString();
        }

        private string OrdersInWorkAmount(int itemId, List<OrdersInWork> ordersInWork)
        {
            int amountItem = 0;
            foreach (OrdersInWork orderInWork in ordersInWork)
            {
                if (orderInWork.Item.Equals(itemId))
                {
                    amountItem += orderInWork.Amount;
                }
            }
            return amountItem.ToString();
            //OrdersInWork orderInWork = ordersInWork.FirstOrDefault(o => o.Item == item);
            //if (orderInWork != null)
            //{
            //    return orderInWork.Amount.ToString();
            //}
            //else
            //{
            //    return "0";
            //}

        }

        public void RibbonButton_Start_P1_Click(object sender, RoutedEventArgs e)
        {

            //P1
            this.txtP1ProdauftrKommendePeriode.Text = (double.Parse(this.txtP1Vertriebswunsch.Text) + double.Parse(this.txtP1GeplLagerbestand.Text) 
                - double.Parse(this.txtP1LagerbestEndeVorperiode.Text) - double.Parse(this.txtP1AuftrInWarteschlange.Text) 
                - double.Parse(this.txtP1AuftrInBearbeitung.Text)).ToString();

            //E26_P1
            if (int.Parse(txtP1ProdauftrKommendePeriode.Text) < 0)
                this.txtE26Vertriebswunsch.Text = "0";
            else
                this.txtE26Vertriebswunsch.Text = this.txtP1ProdauftrKommendePeriode.Text;

            this.txtE26Warteschlange.Text = this.txtP1AuftrInWarteschlange.Text;
            this.txtE26ProdauftrKommendePeriode.Text = (double.Parse(this.txtE26Vertriebswunsch.Text) + double.Parse(this.txtE26Warteschlange.Text) 
                + double.Parse(this.txtE26GeplLagerbestand.Text) - double.Parse(this.txtE26LagerbestEndeVorperiode.Text) 
                - double.Parse(this.txtE26AuftrInWarteschlange.Text) - double.Parse(this.txtE26AuftrInBearbeitung.Text)).ToString();

            //E51
            this.txtE51Vertriebswunsch.Text = this.txtE26Vertriebswunsch.Text;
            this.txtE51Warteschlange.Text = this.txtP1AuftrInWarteschlange.Text;
            this.txtE51ProdauftrKommendePeriode.Text = (double.Parse(this.txtE51Vertriebswunsch.Text) + double.Parse(this.txtE51Warteschlange.Text)
                + double.Parse(this.txtE51GeplLagerbestand.Text) - double.Parse(this.txtE51LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE51AuftrInWarteschlange.Text) - double.Parse(this.txtE51AuftrInBearbeitung.Text)).ToString();
            
            //E16_P1
            if (int.Parse(this.txtE51ProdauftrKommendePeriode.Text) < 0)
                this.txtE16Vertriebswunsch.Text = "0";
            else
                this.txtE16Vertriebswunsch.Text = this.txtE51ProdauftrKommendePeriode.Text;

            this.txtE16Warteschlange.Text = this.txtE51AuftrInWarteschlange.Text;
            this.txtE16ProdauftrKommendePeriode.Text = (double.Parse(this.txtE16Vertriebswunsch.Text) + double.Parse(this.txtE16Warteschlange.Text)
                + double.Parse(this.txtE16GeplLagerbestand.Text) - double.Parse(this.txtE16LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE16AuftrInWarteschlange.Text) - double.Parse(this.txtE16AuftrInBearbeitung.Text)).ToString();

            //E17_P1
            this.txtE17Vertriebswunsch.Text = this.txtE16Vertriebswunsch.Text;
            this.txtE17Warteschlange.Text = this.txtE51AuftrInWarteschlange.Text;
            this.txtE17ProdauftrKommendePeriode.Text = (double.Parse(this.txtE17Vertriebswunsch.Text) + double.Parse(this.txtE17Warteschlange.Text)
                + double.Parse(this.txtE17GeplLagerbestand.Text) - double.Parse(this.txtE17LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE17AuftrInWarteschlange.Text) - double.Parse(this.txtE17AuftrInBearbeitung.Text)).ToString();

            //E50
            this.txtE50Vertriebswunsch.Text = this.txtE16Vertriebswunsch.Text;
            this.txtE50Warteschlange.Text = this.txtE51AuftrInWarteschlange.Text;
            this.txtE50ProdauftrKommendePeriode.Text = (double.Parse(this.txtE50Vertriebswunsch.Text) + double.Parse(this.txtE50Warteschlange.Text)
                + double.Parse(this.txtE50GeplLagerbestand.Text) - double.Parse(this.txtE50LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE50AuftrInWarteschlange.Text) - double.Parse(this.txtE50AuftrInBearbeitung.Text)).ToString();

            //E4
                if (int.Parse(this.txtE50ProdauftrKommendePeriode.Text) < 0)
                    this.txtE04Vertriebswunsch.Text = "0";
                else
                    this.txtE04Vertriebswunsch.Text = this.txtE50ProdauftrKommendePeriode.Text;
            
            this.txtE04Warteschlange.Text = this.txtE50AuftrInWarteschlange.Text;
            this.txtE04ProdauftrKommendePeriode.Text = (double.Parse(this.txtE04Vertriebswunsch.Text) + double.Parse(this.txtE04Warteschlange.Text)
                + double.Parse(this.txtE04GeplLagerbestand.Text) - double.Parse(this.txtE04LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE04AuftrInWarteschlange.Text) - double.Parse(this.txtE04AuftrInBearbeitung.Text)).ToString();

            //E10
            this.txtE10Vertriebswunsch.Text = this.txtE04Vertriebswunsch.Text;
            this.txtE10Warteschlange.Text = this.txtE50AuftrInWarteschlange.Text;
            this.txtE10ProdauftrKommendePeriode.Text = (double.Parse(this.txtE10Vertriebswunsch.Text) + double.Parse(this.txtE10Warteschlange.Text)
                + double.Parse(this.txtE10GeplLagerbestand.Text) - double.Parse(this.txtE10LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE10AuftrInWarteschlange.Text) - double.Parse(this.txtE10AuftrInBearbeitung.Text)).ToString();

            //E49
            this.txtE49Vertriebswunsch.Text = this.txtE04Vertriebswunsch.Text;
            this.txtE49Warteschlange.Text = this.txtE50AuftrInWarteschlange.Text;
            this.txtE49ProdauftrKommendePeriode.Text = (double.Parse(this.txtE49Vertriebswunsch.Text) + double.Parse(this.txtE49Warteschlange.Text)
                + double.Parse(this.txtE49GeplLagerbestand.Text) - double.Parse(this.txtE49LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE49AuftrInWarteschlange.Text) - double.Parse(this.txtE49AuftrInBearbeitung.Text)).ToString();

            //E7
            if (int.Parse(this.txtE49ProdauftrKommendePeriode.Text) < 0)
                this.txtE07Vertriebswunsch.Text = "0";
            else
                this.txtE07Vertriebswunsch.Text = this.txtE49ProdauftrKommendePeriode.Text;

            this.txtE07Warteschlange.Text = this.txtE49AuftrInWarteschlange.Text;
            this.txtE07ProdauftrKommendePeriode.Text = (double.Parse(this.txtE07Vertriebswunsch.Text) + double.Parse(this.txtE07Warteschlange.Text)
                + double.Parse(this.txtE07GeplLagerbestand.Text) - double.Parse(this.txtE07LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE07AuftrInWarteschlange.Text) - double.Parse(this.txtE07AuftrInBearbeitung.Text)).ToString();

            //E13
            this.txtE13Vertriebswunsch.Text = this.txtE07Vertriebswunsch.Text;
            this.txtE13Warteschlange.Text = this.txtE49AuftrInWarteschlange.Text;
            this.txtE13ProdauftrKommendePeriode.Text = (double.Parse(this.txtE13Vertriebswunsch.Text) + double.Parse(this.txtE13Warteschlange.Text)
                + double.Parse(this.txtE13GeplLagerbestand.Text) - double.Parse(this.txtE13LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE13AuftrInWarteschlange.Text) - double.Parse(this.txtE13AuftrInBearbeitung.Text)).ToString();

            //E18
            this.txtE18Vertriebswunsch.Text = this.txtE07Vertriebswunsch.Text;
            this.txtE18Warteschlange.Text = this.txtE49AuftrInWarteschlange.Text;
            this.txtE18ProdauftrKommendePeriode.Text = (double.Parse(this.txtE18Vertriebswunsch.Text) + double.Parse(this.txtE18Warteschlange.Text)
                + double.Parse(this.txtE18GeplLagerbestand.Text) - double.Parse(this.txtE18LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE18AuftrInWarteschlange.Text) - double.Parse(this.txtE18AuftrInBearbeitung.Text)).ToString();

            CreateListOforders();

            

        }


        public void RibbonButton_Start_P2_Click(object sender, RoutedEventArgs e)
        {
            //P2
            this.txtP2ProdauftrKommendePeriode.Text = (double.Parse(this.txtP2Vertriebswunsch.Text) + double.Parse(this.txtP2GeplLagerbestand.Text)
                - double.Parse(this.txtP2LagerbestEndeVorperiode.Text) - double.Parse(this.txtP2AuftrInWarteschlange.Text)
                - double.Parse(this.txtP2AuftrInBearbeitung.Text)).ToString();

            //E26_P2
            //this.txtE26_P2Vertriebswunsch.Text = this.txtP2ProdauftrKommendePeriode.Text;
            //this.txtE26_P2Warteschlange.Text = this.txtP2AuftrInWarteschlange.Text;
            //this.txtE26_P2ProdauftrKommendePeriode.Text = (double.Parse(this.txtE26_P2Vertriebswunsch.Text) + double.Parse(this.txtE26_P2Warteschlange.Text)
            //    + double.Parse(this.txtE26_P2GeplLagerbestand.Text) - double.Parse(this.txtE26_P2LagerbestEndeVorperiode.Text)
            //    - double.Parse(this.txtE26_P2AuftrInWarteschlange.Text) - double.Parse(this.txtE26_P2AuftrInBearbeitung.Text)).ToString();

            if (int.Parse(this.txtP2ProdauftrKommendePeriode.Text) < 0)
                this.txtE26_P2Vertriebswunsch.Text = "0";
            else
                this.txtE26_P2Vertriebswunsch.Text = this.txtP2ProdauftrKommendePeriode.Text;


            this.txtE26_P2Warteschlange.Text = this.txtP2AuftrInWarteschlange.Text;
            this.txtE26_P2ProdauftrKommendePeriode.Text = (double.Parse(this.txtE26_P2Vertriebswunsch.Text) +
                double.Parse(this.txtE26_P2Warteschlange.Text) + double.Parse(this.txtE26_P2GeplLagerbestand.Text)).ToString();

            //E56
            this.txtE56Vertriebswunsch.Text = this.txtE26_P2Vertriebswunsch.Text;
            this.txtE56Warteschlange.Text = this.txtP2AuftrInWarteschlange.Text;
            this.txtE56ProdauftrKommendePeriode.Text = (double.Parse(this.txtE56Vertriebswunsch.Text) + double.Parse(this.txtE56Warteschlange.Text)
                + double.Parse(this.txtE56GeplLagerbestand.Text) - double.Parse(this.txtE56LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE56AuftrInWarteschlange.Text) - double.Parse(this.txtE56AuftrInBearbeitung.Text)).ToString();

            //E16_P2
            //this.txtE16_P2Vertriebswunsch.Text = this.txtE56ProdauftrKommendePeriode.Text;
            //this.txtE16_P2Warteschlange.Text = this.txtE56AuftrInWarteschlange.Text;
            //this.txtE16_P2ProdauftrKommendePeriode.Text = (double.Parse(this.txtE16_P2Vertriebswunsch.Text) + double.Parse(this.txtE16_P2Warteschlange.Text)
            //    + double.Parse(this.txtE16_P2GeplLagerbestand.Text) - double.Parse(this.txtE16_P2LagerbestEndeVorperiode.Text)
            //    - double.Parse(this.txtE16_P2AuftrInWarteschlange.Text) - double.Parse(this.txtE16_P2AuftrInBearbeitung.Text)).ToString();

            if (int.Parse(this.txtE56ProdauftrKommendePeriode.Text) < 0)
                this.txtE16_P2Vertriebswunsch.Text = "0";
            else
                this.txtE16_P2Vertriebswunsch.Text = this.txtE56ProdauftrKommendePeriode.Text;

            this.txtE16_P2Warteschlange.Text = this.txtE56AuftrInWarteschlange.Text;
            this.txtE16_P2ProdauftrKommendePeriode.Text = (double.Parse(this.txtE16_P2Vertriebswunsch.Text) + 
                double.Parse(this.txtE16_P2Warteschlange.Text) + double.Parse(this.txtE16_P2GeplLagerbestand.Text)).ToString();

            //E17_P2
            //this.txtE17_P2Vertriebswunsch.Text = this.txtE56ProdauftrKommendePeriode.Text;
            //this.txtE17_P2Warteschlange.Text = this.txtE56AuftrInWarteschlange.Text;
            //this.txtE17_P2ProdauftrKommendePeriode.Text = (double.Parse(this.txtE17_P2Vertriebswunsch.Text) + double.Parse(this.txtE17_P2Warteschlange.Text)
            //    + double.Parse(this.txtE17_P2GeplLagerbestand.Text) - double.Parse(this.txtE17_P2LagerbestEndeVorperiode.Text)
            //    - double.Parse(this.txtE17_P2AuftrInWarteschlange.Text) - double.Parse(this.txtE17_P2AuftrInBearbeitung.Text)).ToString();

            this.txtE17_P2Vertriebswunsch.Text = this.txtE16_P2Vertriebswunsch.Text;
            this.txtE17_P2Warteschlange.Text = this.txtE56AuftrInWarteschlange.Text;
            this.txtE17_P2ProdauftrKommendePeriode.Text = (double.Parse(this.txtE17_P2Vertriebswunsch.Text) +
                double.Parse(this.txtE17_P2Warteschlange.Text) + double.Parse(this.txtE17_P2GeplLagerbestand.Text)).ToString();

            //E55
            this.txtE55Vertriebswunsch.Text = this.txtE16_P2Vertriebswunsch.Text;
            this.txtE55Warteschlange.Text = this.txtE56AuftrInWarteschlange.Text;
            this.txtE55ProdauftrKommendePeriode.Text = (double.Parse(this.txtE55Vertriebswunsch.Text) + double.Parse(this.txtE55Warteschlange.Text)
                + double.Parse(this.txtE55GeplLagerbestand.Text) - double.Parse(this.txtE55LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE55AuftrInWarteschlange.Text) - double.Parse(this.txtE55AuftrInBearbeitung.Text)).ToString();

            //E5
            if (int.Parse(this.txtE55ProdauftrKommendePeriode.Text) < 0)
                this.txtE05Vertriebswunsch.Text = "0";
            else
                this.txtE05Vertriebswunsch.Text = this.txtE55ProdauftrKommendePeriode.Text;

            this.txtE05Warteschlange.Text = this.txtE55AuftrInWarteschlange.Text;
            this.txtE05ProdauftrKommendePeriode.Text = (double.Parse(this.txtE05Vertriebswunsch.Text) + double.Parse(this.txtE05Warteschlange.Text)
                + double.Parse(this.txtE05GeplLagerbestand.Text) - double.Parse(this.txtE05LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE05AuftrInWarteschlange.Text) - double.Parse(this.txtE05AuftrInBearbeitung.Text)).ToString();

            //E11
            this.txtE11Vertriebswunsch.Text = this.txtE05Vertriebswunsch.Text;
            this.txtE11Warteschlange.Text = this.txtE55AuftrInWarteschlange.Text;
            this.txtE11ProdauftrKommendePeriode.Text = (double.Parse(this.txtE11Vertriebswunsch.Text) + double.Parse(this.txtE11Warteschlange.Text)
                + double.Parse(this.txtE11GeplLagerbestand.Text) - double.Parse(this.txtE11LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE11AuftrInWarteschlange.Text) - double.Parse(this.txtE11AuftrInBearbeitung.Text)).ToString();

            //E54
            this.txtE54Vertriebswunsch.Text = this.txtE05Vertriebswunsch.Text;
            this.txtE54Warteschlange.Text = this.txtE55AuftrInWarteschlange.Text;
            this.txtE54ProdauftrKommendePeriode.Text = (double.Parse(this.txtE54Vertriebswunsch.Text) + double.Parse(this.txtE54Warteschlange.Text)
                + double.Parse(this.txtE54GeplLagerbestand.Text) - double.Parse(this.txtE54LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE54AuftrInWarteschlange.Text) - double.Parse(this.txtE54AuftrInBearbeitung.Text)).ToString();

            //E8
            if (int.Parse(this.txtE54ProdauftrKommendePeriode.Text) < 0)
                this.txtE08Vertriebswunsch.Text = "0";
            else
                this.txtE08Vertriebswunsch.Text = this.txtE54ProdauftrKommendePeriode.Text;

            this.txtE08Warteschlange.Text = this.txtE54AuftrInWarteschlange.Text;
            this.txtE08ProdauftrKommendePeriode.Text = (double.Parse(this.txtE08Vertriebswunsch.Text) + double.Parse(this.txtE08Warteschlange.Text)
                + double.Parse(this.txtE08GeplLagerbestand.Text) - double.Parse(this.txtE08LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE08AuftrInWarteschlange.Text) - double.Parse(this.txtE08AuftrInBearbeitung.Text)).ToString();

            //E14
            this.txtE14Vertriebswunsch.Text = this.txtE08Vertriebswunsch.Text;
            this.txtE14Warteschlange.Text = this.txtE54AuftrInWarteschlange.Text;
            this.txtE14ProdauftrKommendePeriode.Text = (double.Parse(this.txtE14Vertriebswunsch.Text) + double.Parse(this.txtE14Warteschlange.Text)
                + double.Parse(this.txtE14GeplLagerbestand.Text) - double.Parse(this.txtE14LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE14AuftrInWarteschlange.Text) - double.Parse(this.txtE14AuftrInBearbeitung.Text)).ToString();

            //E19
            this.txtE19Vertriebswunsch.Text = this.txtE08Vertriebswunsch.Text;
            this.txtE19Warteschlange.Text = this.txtE54AuftrInWarteschlange.Text;
            this.txtE19ProdauftrKommendePeriode.Text = (double.Parse(this.txtE19Vertriebswunsch.Text) + double.Parse(this.txtE19Warteschlange.Text)
                + double.Parse(this.txtE19GeplLagerbestand.Text) - double.Parse(this.txtE19LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE19AuftrInWarteschlange.Text) - double.Parse(this.txtE19AuftrInBearbeitung.Text)).ToString();

            CreateListOforders();

        }

        public void RibbonButton_Start_P3_Click(object sender, RoutedEventArgs e)
        {
            //P3
            this.txtP3ProdauftrKommendePeriode.Text = (double.Parse(this.txtP3Vertriebswunsch.Text) + double.Parse(this.txtP3GeplLagerbestand.Text)
                - double.Parse(this.txtP3LagerbestEndeVorperiode.Text) - double.Parse(this.txtP3AuftrInWarteschlange.Text)
                - double.Parse(this.txtP3AuftrInBearbeitung.Text)).ToString();

            //E26_P3
            //this.txtE26_P3Vertriebswunsch.Text = this.txtP3ProdauftrKommendePeriode.Text;
            //this.txtE26_P3Warteschlange.Text = this.txtP3AuftrInWarteschlange.Text;
            //this.txtE26_P3ProdauftrKommendePeriode.Text = (double.Parse(this.txtE26_P3Vertriebswunsch.Text) + double.Parse(this.txtE26_P3Warteschlange.Text)
            //    + double.Parse(this.txtE26_P3GeplLagerbestand.Text) - double.Parse(this.txtE26_P3LagerbestEndeVorperiode.Text)
            //    - double.Parse(this.txtE26_P3AuftrInWarteschlange.Text) - double.Parse(this.txtE26_P3AuftrInBearbeitung.Text)).ToString();

            if (int.Parse(this.txtP3ProdauftrKommendePeriode.Text) < 0)
                this.txtE26_P3Vertriebswunsch.Text = "0";
            else
                this.txtE26_P3Vertriebswunsch.Text = this.txtP3ProdauftrKommendePeriode.Text;

            this.txtE26_P3Warteschlange.Text = this.txtP3AuftrInWarteschlange.Text;
            this.txtE26_P3ProdauftrKommendePeriode.Text = (double.Parse(this.txtE26_P3Vertriebswunsch.Text) +
                double.Parse(this.txtE26_P3Warteschlange.Text) + double.Parse(this.txtE26_P3GeplLagerbestand.Text)).ToString();

            //E31
            this.txtE31Vertriebswunsch.Text = this.txtE26_P3Vertriebswunsch.Text;
            this.txtE31Warteschlange.Text = this.txtP3AuftrInWarteschlange.Text;
            this.txtE31ProdauftrKommendePeriode.Text = (double.Parse(this.txtE31Vertriebswunsch.Text) + double.Parse(this.txtE31Warteschlange.Text)
                + double.Parse(this.txtE31GeplLagerbestand.Text) - double.Parse(this.txtE31LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE31AuftrInWarteschlange.Text) - double.Parse(this.txtE31AuftrInBearbeitung.Text)).ToString();

            //E16_P3
            //this.txtE16_P3Vertriebswunsch.Text = this.txtE31ProdauftrKommendePeriode.Text;
            //this.txtE16_P3Warteschlange.Text = this.txtE31AuftrInWarteschlange.Text;
            //this.txtE16_P3ProdauftrKommendePeriode.Text = (double.Parse(this.txtE16_P3Vertriebswunsch.Text) + double.Parse(this.txtE16_P3Warteschlange.Text)
            //    + double.Parse(this.txtE16_P3GeplLagerbestand.Text) - double.Parse(this.txtE16_P3LagerbestEndeVorperiode.Text)
            //    - double.Parse(this.txtE16_P3AuftrInWarteschlange.Text) - double.Parse(this.txtE16_P3AuftrInBearbeitung.Text)).ToString();
            if (int.Parse(this.txtE31ProdauftrKommendePeriode.Text) < 0)
                this.txtE16_P3Vertriebswunsch.Text = "0";
            else
                this.txtE16_P3Vertriebswunsch.Text = this.txtE31ProdauftrKommendePeriode.Text;

            this.txtE16_P3Warteschlange.Text = this.txtE31AuftrInWarteschlange.Text;
            this.txtE16_P3ProdauftrKommendePeriode.Text = (double.Parse(this.txtE16_P3Vertriebswunsch.Text) +
                double.Parse(this.txtE16_P3Warteschlange.Text) + double.Parse(this.txtE16_P3GeplLagerbestand.Text)).ToString();

            //E17_P3
            //this.txtE17_P3Vertriebswunsch.Text = this.txtE31ProdauftrKommendePeriode.Text;
            //this.txtE17_P3Warteschlange.Text = this.txtE31AuftrInWarteschlange.Text;
            //this.txtE17_P3ProdauftrKommendePeriode.Text = (double.Parse(this.txtE17_P3Vertriebswunsch.Text) + double.Parse(this.txtE17_P3Warteschlange.Text)
            //    + double.Parse(this.txtE17_P3GeplLagerbestand.Text) - double.Parse(this.txtE17_P3LagerbestEndeVorperiode.Text)
            //    - double.Parse(this.txtE17_P3AuftrInWarteschlange.Text) - double.Parse(this.txtE17_P3AuftrInBearbeitung.Text)).ToString();

            this.txtE17_P3Vertriebswunsch.Text = this.txtE16_P3Vertriebswunsch.Text;
            this.txtE17_P3Warteschlange.Text = this.txtE31AuftrInWarteschlange.Text;
            this.txtE17_P3ProdauftrKommendePeriode.Text = (double.Parse(this.txtE17_P3Vertriebswunsch.Text) +
                double.Parse(this.txtE17_P3Warteschlange.Text) + double.Parse(this.txtE17_P3GeplLagerbestand.Text)).ToString();
            
            //E30
            this.txtE30Vertriebswunsch.Text = this.txtE16_P3Vertriebswunsch.Text;
            this.txtE30Warteschlange.Text = this.txtE31AuftrInWarteschlange.Text;
            this.txtE30ProdauftrKommendePeriode.Text = (double.Parse(this.txtE30Vertriebswunsch.Text) + double.Parse(this.txtE30Warteschlange.Text)
                + double.Parse(this.txtE30GeplLagerbestand.Text) - double.Parse(this.txtE30LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE30AuftrInWarteschlange.Text) - double.Parse(this.txtE30AuftrInBearbeitung.Text)).ToString();

            //E6
            if (int.Parse(this.txtE30ProdauftrKommendePeriode.Text) < 0)
                this.txtE06Vertriebswunsch.Text = "0";
            else
                this.txtE06Vertriebswunsch.Text = this.txtE30ProdauftrKommendePeriode.Text;

            this.txtE06Warteschlange.Text = this.txtE30AuftrInWarteschlange.Text;
            this.txtE06ProdauftrKommendePeriode.Text = (double.Parse(this.txtE06Vertriebswunsch.Text) + double.Parse(this.txtE06Warteschlange.Text)
                + double.Parse(this.txtE06GeplLagerbestand.Text) - double.Parse(this.txtE06LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE06AuftrInWarteschlange.Text) - double.Parse(this.txtE06AuftrInBearbeitung.Text)).ToString();

            //E12
            this.txtE12Vertriebswunsch.Text = this.txtE06Vertriebswunsch.Text;
            this.txtE12Warteschlange.Text = this.txtE30AuftrInWarteschlange.Text;
            this.txtE12ProdauftrKommendePeriode.Text = (double.Parse(this.txtE12Vertriebswunsch.Text) + double.Parse(this.txtE12Warteschlange.Text)
                + double.Parse(this.txtE12GeplLagerbestand.Text) - double.Parse(this.txtE12LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE12AuftrInWarteschlange.Text) - double.Parse(this.txtE12AuftrInBearbeitung.Text)).ToString();

            //E29
            this.txtE29Vertriebswunsch.Text = this.txtE06Vertriebswunsch.Text;
            this.txtE29Warteschlange.Text = this.txtE30AuftrInWarteschlange.Text;
            this.txtE29ProdauftrKommendePeriode.Text = (double.Parse(this.txtE29Vertriebswunsch.Text) + double.Parse(this.txtE29Warteschlange.Text)
                + double.Parse(this.txtE29GeplLagerbestand.Text) - double.Parse(this.txtE29LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE29AuftrInWarteschlange.Text) - double.Parse(this.txtE29AuftrInBearbeitung.Text)).ToString();

            //E9
            if (int.Parse(this.txtE29ProdauftrKommendePeriode.Text) < 0)
                this.txtE09Vertriebswunsch.Text = "0";
            else
                this.txtE09Vertriebswunsch.Text = this.txtE29ProdauftrKommendePeriode.Text;

            this.txtE09Warteschlange.Text = this.txtE29AuftrInWarteschlange.Text;
            this.txtE09ProdauftrKommendePeriode.Text = (double.Parse(this.txtE09Vertriebswunsch.Text) + double.Parse(this.txtE09Warteschlange.Text)
                + double.Parse(this.txtE09GeplLagerbestand.Text) - double.Parse(this.txtE09LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE09AuftrInWarteschlange.Text) - double.Parse(this.txtE09AuftrInBearbeitung.Text)).ToString();

            //E15
            this.txtE15Vertriebswunsch.Text = this.txtE09Vertriebswunsch.Text;
            this.txtE15Warteschlange.Text = this.txtE29AuftrInWarteschlange.Text;
            this.txtE15ProdauftrKommendePeriode.Text = (double.Parse(this.txtE15Vertriebswunsch.Text) + double.Parse(this.txtE15Warteschlange.Text)
                + double.Parse(this.txtE15GeplLagerbestand.Text) - double.Parse(this.txtE15LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE15AuftrInWarteschlange.Text) - double.Parse(this.txtE15AuftrInBearbeitung.Text)).ToString();

            //E20
            this.txtE20Vertriebswunsch.Text = this.txtE09Vertriebswunsch.Text;
            this.txtE20Warteschlange.Text = this.txtE29AuftrInWarteschlange.Text;
            this.txtE20ProdauftrKommendePeriode.Text = (double.Parse(this.txtE20Vertriebswunsch.Text) + double.Parse(this.txtE20Warteschlange.Text)
                + double.Parse(this.txtE20GeplLagerbestand.Text) - double.Parse(this.txtE20LagerbestEndeVorperiode.Text)
                - double.Parse(this.txtE20AuftrInWarteschlange.Text) - double.Parse(this.txtE20AuftrInBearbeitung.Text)).ToString();

            CreateListOforders();

        }


        public void Button_Click_P1(object sender, RoutedEventArgs e)
        {
            this.txtE26GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE51GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE16GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE17GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE50GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE04GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE10GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE49GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE07GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE13GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            this.txtE18GeplLagerbestand.Text = this.txtP1GeplLagerbestand.Text;
            KeyUp(null, null);
            
        }

        public void Button_Click_P2(object sender, RoutedEventArgs e)
        {
            this.txtE26_P2GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE56GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE16_P2GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE17_P2GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE55GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE05GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE11GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE54GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE08GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE14GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            this.txtE19GeplLagerbestand.Text = this.txtP2GeplLagerbestand.Text;
            KeyUp(null, null);
            
        }

        public void Button_Click_P3(object sender, RoutedEventArgs e)
        {
            this.txtE26_P3GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE31GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE16_P3GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE17_P3GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE30GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE06GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE12GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE29GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE09GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE15GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            this.txtE20GeplLagerbestand.Text = this.txtP3GeplLagerbestand.Text;
            KeyUp(null, null);
            
        }

        
        public List<Order> CreateListOforders ()
        {
           List<Order> listOfOrders = new List<Order>();
           listOfOrders.Add(CreateOrder(1, int.Parse(this.txtP1ProdauftrKommendePeriode.Text), 1, "E"));

           int E26P01 = 0;
           int E26P02 = 0;
           int E26P03 = 0;


           E26P01 = int.Parse(txtE26ProdauftrKommendePeriode.Text);
           E26P02 = int.Parse(txtE26_P2ProdauftrKommendePeriode.Text);
           E26P03 = int.Parse(txtE26_P3ProdauftrKommendePeriode.Text);

           int resultE26 = E26P01 + E26P02 + E26P03;
           listOfOrders.Add(CreateOrder(26, resultE26, 26, "M"));

           listOfOrders.Add(CreateOrder(51, int.Parse(this.txtE51ProdauftrKommendePeriode.Text), 51, "M"));

           int E16P01 = 0;
           int E16P02 = 0;
           int E16P03 = 0;
            

                E16P01 = int.Parse(txtE16ProdauftrKommendePeriode.Text);
                E16P02 = int.Parse(txtE16_P2ProdauftrKommendePeriode.Text);
                E16P03 = int.Parse(txtE16_P3ProdauftrKommendePeriode.Text);

           int resultE16 = E16P01 + E16P02 + E16P03;           
           listOfOrders.Add(CreateOrder(16, resultE16, 16, "M"));

           int E17P01 = 0;
           int E17P02 = 0;
           int E17P03 = 0;

               E17P01 = int.Parse(txtE17ProdauftrKommendePeriode.Text);
               E17P02 = int.Parse(txtE17_P2ProdauftrKommendePeriode.Text);
               E17P03 = int.Parse(txtE17_P3ProdauftrKommendePeriode.Text);

           int resultE17 = E16P01 + E16P02 + E16P03;
           listOfOrders.Add(CreateOrder(17, resultE17, 17, "M"));

           listOfOrders.Add(CreateOrder(50, int.Parse(this.txtE50ProdauftrKommendePeriode.Text), 50, "M"));
           listOfOrders.Add(CreateOrder(4, int.Parse(this.txtE04ProdauftrKommendePeriode.Text), 4, "M"));
           listOfOrders.Add(CreateOrder(10, int.Parse(this.txtE10ProdauftrKommendePeriode.Text), 10, "M"));
           listOfOrders.Add(CreateOrder(49, int.Parse(this.txtE49ProdauftrKommendePeriode.Text), 49, "M"));
           listOfOrders.Add(CreateOrder(7, int.Parse(this.txtE07ProdauftrKommendePeriode.Text), 7, "M"));
           listOfOrders.Add(CreateOrder(13, int.Parse(this.txtE13ProdauftrKommendePeriode.Text), 13, "M"));
           listOfOrders.Add(CreateOrder(18, int.Parse(this.txtE18ProdauftrKommendePeriode.Text), 18, "M"));

           listOfOrders.Add(CreateOrder(2, int.Parse(this.txtP2ProdauftrKommendePeriode.Text), 2, "E"));
           listOfOrders.Add(CreateOrder(56, int.Parse(this.txtE56ProdauftrKommendePeriode.Text), 56, "M"));
           listOfOrders.Add(CreateOrder(55, int.Parse(this.txtE55ProdauftrKommendePeriode.Text), 55, "M"));
           listOfOrders.Add(CreateOrder(5, int.Parse(this.txtE05ProdauftrKommendePeriode.Text), 5, "M"));
           listOfOrders.Add(CreateOrder(11, int.Parse(this.txtE11ProdauftrKommendePeriode.Text), 11, "M"));
           listOfOrders.Add(CreateOrder(54, int.Parse(this.txtE54ProdauftrKommendePeriode.Text), 54, "M"));
           listOfOrders.Add(CreateOrder(8, int.Parse(this.txtE08ProdauftrKommendePeriode.Text), 8, "M"));
           listOfOrders.Add(CreateOrder(14, int.Parse(this.txtE14ProdauftrKommendePeriode.Text), 14, "M"));
           listOfOrders.Add(CreateOrder(19, int.Parse(this.txtE19ProdauftrKommendePeriode.Text), 19, "M"));

           listOfOrders.Add(CreateOrder(3, int.Parse(this.txtP3ProdauftrKommendePeriode.Text), 3, "E"));
           listOfOrders.Add(CreateOrder(31, int.Parse(this.txtE31ProdauftrKommendePeriode.Text), 31, "M"));
           listOfOrders.Add(CreateOrder(30, int.Parse(this.txtE30ProdauftrKommendePeriode.Text), 30, "M"));
           listOfOrders.Add(CreateOrder(6, int.Parse(this.txtE06ProdauftrKommendePeriode.Text), 6, "M"));
           listOfOrders.Add(CreateOrder(12, int.Parse(this.txtE12ProdauftrKommendePeriode.Text), 12, "M"));
           listOfOrders.Add(CreateOrder(29, int.Parse(this.txtE29ProdauftrKommendePeriode.Text), 29, "M"));
           listOfOrders.Add(CreateOrder(9, int.Parse(this.txtE09ProdauftrKommendePeriode.Text), 9, "M"));
           listOfOrders.Add(CreateOrder(15, int.Parse(this.txtE15ProdauftrKommendePeriode.Text), 15, "M"));
           listOfOrders.Add(CreateOrder(20, int.Parse(this.txtE20ProdauftrKommendePeriode.Text), 20, "M"));

           //FindPageControl(this).setButtonsEnable();
           dgOrders.ItemsSource = listOfOrders;
           //FindPageControl(this).Orders = listOfOrders;
           return listOfOrders;
        }

        private Order CreateOrder(int id, int amount, int productId, string typeOfProduct)
        {
            Order order = new Order();
            order.Amount = amount;
            order.Id = id;
            if (typeOfProduct == "M")
                order.Product = manufacturedParts.FirstOrDefault(o => o.Id == productId);
            else
                order.Product =  endproducts.FirstOrDefault(o => o.Id == id);

            return order;
        }

        public static MainWindow FindPageControl(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);

            if (parent == null) return null;

            MainWindow page = parent as MainWindow;
            if (page != null)
            {
                return page;
            }
            else
            {
                return FindPageControl(parent);
            }
        }


        private void RibbonButton_nextTab1_Click(object sender, RoutedEventArgs e)
        {
            tabControl.SelectedIndex = 1;
        }

        private void RibbonButton_nextTab2_Click(object sender, RoutedEventArgs e)
        {
            tabControl.SelectedIndex = 2;
        }

        private void RibbonButton_nextTab3_Click(object sender, RoutedEventArgs e)
        {
            fertig = 1;
            KeyUp(null, null);
            FindPageControl(this).btProductionPlanning.IsChecked = false;
            FindPageControl(this).btOrderPlanning.IsChecked = true;
            FindPageControl(this).setButtonsEnabledAfterProdPlanning();
            FindPageControl(this).ChangeViewTo(MainWindow.VIEWTYPE.ORDER);
            
        }

        private void RibbonButton_backTab1_Click(object sender, RoutedEventArgs e)
        {
            FindPageControl(this).btProductionPlanning.IsChecked = false;
            FindPageControl(this).ChangeViewTo(MainWindow.VIEWTYPE.FORECAST); 
        }

        private void RibbonButton_backTab2_Click(object sender, RoutedEventArgs e)
        {
            tabControl.SelectedIndex = 0;
        }

        private void RibbonButton_backTab3_Click(object sender, RoutedEventArgs e)
        {

            tabControl.SelectedIndex = 1;
        }



        private void KeyUp(object sender, KeyEventArgs e)
        {
           
            //tab1

            if (this.txtP1GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtP1GeplLagerbestand.Text)))
            {
                this.txtP1GeplLagerbestand.Text = "0";
            }

            if (this.txtE26GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE26GeplLagerbestand.Text)))
            {
                this.txtE26GeplLagerbestand.Text = "0";
            }

            if (this.txtE51GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE51GeplLagerbestand.Text)))
            {
                this.txtE51GeplLagerbestand.Text = "0";
            }

            if (this.txtE16GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE16GeplLagerbestand.Text)))
            {
                this.txtE16GeplLagerbestand.Text = "0";
            }

            if (this.txtE17GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE17GeplLagerbestand.Text)))
            {
                this.txtE17GeplLagerbestand.Text = "0";
            }

            if (this.txtE50GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE50GeplLagerbestand.Text)))
            {
                this.txtE50GeplLagerbestand.Text = "0";
            }

            if (this.txtE04GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE04GeplLagerbestand.Text)))
            {
                this.txtE04GeplLagerbestand.Text = "0";
            }

            if (this.txtE10GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE10GeplLagerbestand.Text)))
            {
                this.txtE10GeplLagerbestand.Text = "0";
            }

            if (this.txtE49GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE49GeplLagerbestand.Text)))
            {
                this.txtE49GeplLagerbestand.Text = "0";
            }

            if (this.txtE07GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE07GeplLagerbestand.Text)))
            {
                this.txtE07GeplLagerbestand.Text = "0";
            }

            if (this.txtE13GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE13GeplLagerbestand.Text)))
            {
                this.txtE13GeplLagerbestand.Text = "0";
            }

            if (this.txtE18GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE18GeplLagerbestand.Text)))
            {
                this.txtE18GeplLagerbestand.Text = "0";
            }

            //tab2
            if (this.txtP2GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtP2GeplLagerbestand.Text)))
            {
                this.txtP2GeplLagerbestand.Text = "0";
            }

            if (this.txtE26_P2GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE26_P2GeplLagerbestand.Text)))
            {
                this.txtE26_P2GeplLagerbestand.Text = "0";
            }

            if (this.txtE56GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE56GeplLagerbestand.Text)))
            {
                this.txtE56GeplLagerbestand.Text = "0";
            }

            if (this.txtE18GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE18GeplLagerbestand.Text)))
            {
                this.txtE18GeplLagerbestand.Text = "0";
            }

            if (this.txtE16_P2GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE16_P2GeplLagerbestand.Text)))
            {
                this.txtE16_P2GeplLagerbestand.Text = "0";
            }

            if (this.txtE17_P2GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE17_P2GeplLagerbestand.Text)))
            {
                this.txtE17_P2GeplLagerbestand.Text = "0";
            }

            if (this.txtE55GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE55GeplLagerbestand.Text)))
            {
                this.txtE55GeplLagerbestand.Text = "0";
            }

            if (this.txtE05GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE05GeplLagerbestand.Text)))
            {
                this.txtE05GeplLagerbestand.Text = "0";
            }

            if (this.txtE11GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE11GeplLagerbestand.Text)))
            {
                this.txtE11GeplLagerbestand.Text = "0";
            }

            if (this.txtE54GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE54GeplLagerbestand.Text)))
            {
                this.txtE54GeplLagerbestand.Text = "0";
            }

            if (this.txtE08GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE08GeplLagerbestand.Text)))
            {
                this.txtE08GeplLagerbestand.Text = "0";
            }

            if (this.txtE14GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE14GeplLagerbestand.Text)))
            {
                this.txtE14GeplLagerbestand.Text = "0";
            }

            if (this.txtE19GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE19GeplLagerbestand.Text)))
            {
                this.txtE19GeplLagerbestand.Text = "0";
            }

            //tab3
            if (this.txtP3GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtP3GeplLagerbestand.Text)))
            {
                this.txtP3GeplLagerbestand.Text = "0"; 
            }

            if (this.txtE26_P3GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE26_P3GeplLagerbestand.Text)))
            {
                this.txtE26_P3GeplLagerbestand.Text = "0";
            }

            if (this.txtE31GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE31GeplLagerbestand.Text)))
            {
                this.txtE31GeplLagerbestand.Text = "0";
            }

            if (this.txtE16_P3GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE16_P3GeplLagerbestand.Text)))
            {
                this.txtE16_P3GeplLagerbestand.Text = "0";
            }

            if (this.txtE17_P3GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE17_P3GeplLagerbestand.Text)))
            {
                this.txtE17_P3GeplLagerbestand.Text = "0";
            }

            if (this.txtE30GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE30GeplLagerbestand.Text)))
            {
                this.txtE30GeplLagerbestand.Text = "0";
            }

            if (this.txtE06GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE06GeplLagerbestand.Text)))
            {
                this.txtE06GeplLagerbestand.Text = "0";
            }

            if (this.txtE12GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE12GeplLagerbestand.Text)))
            {
                this.txtE12GeplLagerbestand.Text = "0";
            }

            if (this.txtE29GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE29GeplLagerbestand.Text)))
            {
                this.txtE29GeplLagerbestand.Text = "0";
            }

            if (this.txtE09GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE09GeplLagerbestand.Text)))
            {
                this.txtE09GeplLagerbestand.Text = "0";
            }

            if (this.txtE15GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE15GeplLagerbestand.Text)))
            {
                this.txtE15GeplLagerbestand.Text = "0";
            }

            if (this.txtE20GeplLagerbestand.Text.Equals("") || (!isValueNumeric(this.txtE20GeplLagerbestand.Text)))
            {
                this.txtE20GeplLagerbestand.Text = "0";
            }


            
            RibbonButton_Start_P1_Click(null, null);
            RibbonButton_Start_P2_Click(null, null);
            RibbonButton_Start_P3_Click(null, null);


        }

        private static bool isValueNumeric(string text)
        {
            Regex regex = new Regex("[^0-9.]+"); //regex that matches disallowed text
            return !regex.IsMatch(text);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            List<Forecast> forecasts;
            forecasts = FindPageControl(this).Forecasts;


            Forecast forecast = forecasts.FirstOrDefault(o => o.Product.Id == 1);
            this.txtP1Vertriebswunsch.Text = forecast.Count.ToString();

            Forecast forecast2 = forecasts.FirstOrDefault(o => o.Product.Id == 2);
            this.txtP2Vertriebswunsch.Text = forecast2.Count.ToString();

            Forecast forecast3 = forecasts.FirstOrDefault(o => o.Product.Id == 3);
            this.txtP3Vertriebswunsch.Text = forecast3.Count.ToString();

            KeyUp(null, null);

            if (fertig == 1)
            {

                lblWarningTab1.Visibility = Visibility.Visible;
                lblWarningTab1_Copy.Visibility = Visibility.Visible;
                lblWarningTab1_Copy1.Visibility = Visibility.Visible;
                
            }
            else
            {
                lblWarningTab1.Visibility = Visibility.Hidden;
                lblWarningTab1_Copy.Visibility = Visibility.Hidden;
                lblWarningTab1_Copy1.Visibility = Visibility.Hidden;

            }


        }

        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            
            if (fertig == 1)
            {
                FindPageControl(this).setButtonsDisabledAfterChangedProdPlanning();
                FindPageControl(this).ProductionOrderView = null;
                FindPageControl(this).CapacityPlanView = null;
                FindPageControl(this).PurchasingView = null;
            }
            fertig = 0;
        }

        
    }
}
